<template>
    <div class="weather">
        <!-- 加载状态 -->
        <div v-if="loading" class="loading">
            <p>正在获取天气信息...</p>
        </div>
        

        
        <!-- 当前天气 -->
        <div v-if="weatherData?.current" class="current-weather">
            <div class="city-name">{{ weatherData.current.city }}</div>
            <div class="current-info">
                <div class="current-temp">{{ weatherData.current.temperature }}°C</div>
                <div class="current-condition">
                    <img :src="getWeatherIconPath(weatherData.current.weather)" :alt="weatherData.current.weather">
                    <span>{{ weatherData.current.weather }}</span>
                </div>
            </div>
            <div class="current-details">
                <span>湿度: {{ weatherData.current.humidity }}%</span>
                <span>{{ weatherData.current.winddirection }}风 {{ weatherData.current.windpower }}级</span>
            </div>
            <div class="update-time">更新时间: {{ formatUpdateTime(weatherData.current.reporttime) }}</div>
        </div>
        
        <!-- 天气预报 -->
        <div v-if="weatherData?.forecast?.length" class="forecast-list">
            <div class="forecast-title">天气预报</div>
            <div 
                v-for="(forecast, index) in weatherData.forecast.slice(0, 4)" 
                :key="forecast.date"
                class="weather-info"
            >
                <div class="date">
                    <div class="date-text">{{ formatDate(forecast.date) }}</div>
                    <div class="week-text">{{ getDayLabel(index) }}</div>
                </div>
                <div class="weather-condition">
                    <img :src="getWeatherIconPath(forecast.dayweather)" :alt="forecast.dayweather">
                    <p>{{ forecast.dayweather }}</p>
                </div>
                <div class="temperature">{{ forecast.nighttemp }}-{{ forecast.daytemp }}℃</div>
            </div>
        </div>
        
        <!-- 无数据状态 -->
        <div v-else-if="!loading" class="no-data">
            <p>暂无天气数据</p>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import { getWeatherIcon, formatDate as formatDateUtil, type WeatherData } from '../../api/weatherService';

// 定义props
const props = defineProps<{
  weather?: WeatherData;
}>();

// 响应式数据
const loading = ref(false);
const error = ref('');
const weatherData = ref<WeatherData | null>(null);

// 监听props变化，当有新的天气数据传入时更新
watch(() => props.weather, (newWeather) => {
  if (newWeather) {
    console.log('接收到新的天气数据:', newWeather);
    weatherData.value = newWeather;
    error.value = '';
    loading.value = false;
  }
}, { immediate: true });

// 获取天气图标路径
const getWeatherIconPath = (weather: string): string => {
    const iconName = getWeatherIcon(weather);
    // 直接返回assets路径，让Vite处理
    return `/src/assets/${iconName}`;
};

// 格式化日期
const formatDate = (dateStr: string): string => {
    return formatDateUtil(dateStr);
};

// 获取日期标签
const getDayLabel = (index: number): string => {
    const labels = ['今天', '明天', '后天', '大后天'];
    return labels[index] || '';
};

// 格式化更新时间
const formatUpdateTime = (reporttime?: string): string => {
    if (!reporttime) return '';
    
    try {
        // reporttime格式: "2025-09-24 16:00:00"
        const date = new Date(reporttime.replace(' ', 'T'));
        return date.toLocaleString('zh-CN', {
            month: '2-digit',
            day: '2-digit',
            hour: '2-digit',
            minute: '2-digit'
        });
    } catch (error) {
        return reporttime;
    }
};


</script>

<style scoped lang="less"> 
    .weather{
        background: linear-gradient(to bottom left, #2c305b, #6b6f91);
        color: #fff;
        border-radius: 15px;
        margin-bottom: 10px;
        padding: 20px;
        box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
        
        // 加载状态
        .loading {
            text-align: center;
            padding: 40px 20px;
            font-size: 16px;
            opacity: 0.8;
        }
        

        
        // 当前天气
        .current-weather {
            text-align: center;
            margin-bottom: 25px;
            padding: 20px;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 12px;
            
            .city-name {
                font-size: 24px;
                font-weight: bold;
                margin-bottom: 15px;
            }
            
            .current-info {
                display: flex;
                justify-content: center;
                align-items: center;
                gap: 20px;
                margin-bottom: 15px;
                
                .current-temp {
                    font-size: 48px;
                    font-weight: bold;
                }
                
                .current-condition {
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    gap: 8px;
                    
                    img {
                        width: 50px;
                        height: 50px;
                    }
                    
                    span {
                        font-size: 18px;
                    }
                }
            }
            
            .current-details {
                display: flex;
                justify-content: center;
                gap: 30px;
                margin-bottom: 10px;
                font-size: 14px;
                opacity: 0.9;
            }
            
            .update-time {
                font-size: 12px;
                opacity: 0.7;
            }
        }
        
        // 预报标题
        .forecast-title {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 15px;
            text-align: center;
        }
        
        // 预报列表
        .forecast-list {
            .weather-info{
                display: flex;
                justify-content: space-between;
                align-items: center;
                padding: 15px;
                margin-bottom: 8px;
                background: rgba(255, 255, 255, 0.1);
                border-radius: 10px;
                transition: all 0.3s ease;
                
                &:hover {
                    background: rgba(255, 255, 255, 0.15);
                    transform: translateY(-2px);
                }
                
                .date{
                    flex: 1;
                    text-align: center;
                    
                    .date-text {
                        font-size: 16px;
                        font-weight: bold;
                    }
                    
                    .week-text {
                        font-size: 12px;
                        opacity: 0.8;
                        margin-top: 2px;
                    }
                }
                
                .weather-condition{
                    flex: 1;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    gap: 8px;
                    
                    p{
                        margin: 0;
                        font-size: 14px;
                    }
                    
                    img{
                        width: 32px;
                        height: 32px;
                    }
                }
                
                .temperature{
                    flex: 1;
                    text-align: center;
                    font-size: 16px;
                    font-weight: bold;
                }
            }
        }
        
        // 无数据状态
        .no-data {
            text-align: center;
            padding: 40px 20px;
            font-size: 16px;
            opacity: 0.7;
        }
    }
    
    // 响应式设计
    @media (max-width: 768px) {
        .weather {
            padding: 15px;
            
            .current-weather {
                .current-info {
                    flex-direction: column;
                    gap: 15px;
                    
                    .current-temp {
                        font-size: 36px;
                    }
                }
                
                .current-details {
                    flex-direction: column;
                    gap: 10px;
                }
            }
            
            .forecast-list .weather-info {
                padding: 12px;
                
                .date .date-text {
                    font-size: 14px;
                }
                
                .weather-condition {
                    img {
                        width: 28px;
                        height: 28px;
                    }
                    
                    p {
                        font-size: 12px;
                    }
                }
                
                .temperature {
                    font-size: 14px;
                }
            }
        }
    }
</style>